I <3 Intel
nummish <nummish0x90org> Saturday, July 23 2005 17:21.53 CDT


The documentation is ok. But every so often you find something that forces you to ask questions, and you're only comfort is a manual so large it had to be split into two very large manuals that are totally inconsistent between revisions and really don't have a solution to wtf you wanted to know in the first place.

For example:

An instruction on the intel chipset can be from one to about 15 bytes long. Or anywhere inbetween. Fine. No problem.

[prefixes][opcode][mod r/m][sib][other crap..]

makes sense.. I can deal with that.. so start off with the first less than obvious part:

Mod R/M: (one byte)

[Mod - 2 bits][Reg - 3 bits][R/M - 3 bits]

... ok, so the way this works is that the Reg field always indicates which one of 8 registers is being referenced. if Mod == 3 (11b) Then the R/M field is another register indicator.

So.. if your operands are "r32, r/m32" then the r32 portion is the reg field and the r/m32 is the r/m field. if it happens to be two registers, then mod is 3, as I already stated.

Now.. if your operands are "r/m32, r32" then it's the same, the only difference here is that your destination and source is supplied by the opposite fields as before. But they're read from the same fields.. makes perfect sense.

Until.. if your operands are "r32, r32" .. what.. the.. fuck.. like seriously, there's about 10 instructions that have a format similar to this. You would think that it would be explained clearly or upfront somewhere. After a brief googling, I found some guy who referred to the Mod R/M byte as being "cryptically named", at which point I lost my faith in humanity on the internet and decided to break out nasm and just compile them.

From what I can guess, the rule for Mod R/M goes like this:
if Dest == register and Src == reg/mem -> Dest == reg Src == r/m
if Dest == reg/mem and Src == register -> Dest == r/m Src == reg
if Dest == register and AnythingElse == register -> Dest == reg AnythingElse == r/m

I may have missed the brief sentence where that was explained somewhere, but really it's a completeness thing that annoyed the crap out of me last night. If I'm wrong about this, please tell me.

Comments
Posted: Wednesday, December 31 1969 18:00.00 CST